package com.hehe.boot.controller;

import com.hehe.boot.pojo.Book;
import com.hehe.boot.pojo.BooklHttp.BookLoginRequest;
import com.hehe.boot.pojo.BooklHttp.BookLoginResponse;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.pojo.User;
import com.hehe.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/book")
public class BookController {
    private static final Map<Integer, String> IDENTITY_MAP = new HashMap<>();

    static {
        IDENTITY_MAP.put(1,"Admin");
        IDENTITY_MAP.put(2,"Doctor");
        IDENTITY_MAP.put(3,"User");
    }

    @Autowired
    private BookService bookService;

    @PostMapping("/login")
    public Result<BookLoginResponse> Login(@RequestBody BookLoginRequest request){
        String username = request.getUsername();
        Book film = bookService.findBookByName(username);
        if (film != null && film.getPassword().equals(request.getPassword())){
            System.out.println("handler login");
            BookLoginResponse response = new BookLoginResponse();
            response.setId(film.getRelatedId());
            response.setIdentity(IDENTITY_MAP.get(film.getIdentity()));
            return Result.success(response);
        }else {
            return Result.error("fail to login");
        }
    }
}
